package com.ma.fees;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;

import com.ma.R;

import java.util.ArrayList;
import java.util.List;

import base.BaseActivity;
import lomasky.xui.BaseViewHolder;
import lomasky.xui.RecyclerViewAdapter;
import lomasky.xui.WrapContentLinearLayoutManager;

public class FeesActivity extends BaseActivity {

    private RecyclerView mRecyclerView;
    private CheckBox mCheck;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fees);
        initView();
    }

    private void initView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        mRecyclerView.setLayoutManager(new WrapContentLinearLayoutManager(this));
        RecyclerViewAdapter<Fees> adapter = new RecyclerViewAdapter<Fees>(this, R.layout.fees_item) {
            @Override
            public void onBind(BaseViewHolder holder, Fees item, int position) {

                holder.getCheckBox(R.id.check).setChecked(item.isCheck());
                holder.setText(R.id.name, item.getName());
                holder.setText(R.id.lateFees, item.getLateFees());
                holder.setText(R.id.capital, item.getCapital());
                holder.setText(R.id.total, item.getTotal());
                holder.getCheckBox(R.id.check).setOnCheckedChangeListener((compoundButton, b) -> {
                    holder.getTextView(R.id.name).setTextColor(getResources().getColor(b ? R.color.colorDanger : R.color.dark));
                    holder.getTextView(R.id.lateFees).setTextColor(getResources().getColor(b ? R.color.colorDanger : R.color.dark));
                    holder.getTextView(R.id.total).setTextColor(getResources().getColor(b ? R.color.colorDanger : R.color.dark));
                    holder.getTextView(R.id.capital).setTextColor(getResources().getColor(b ? R.color.colorDanger : R.color.dark));
                });

            }
        };

        List<Fees> fees = new ArrayList<>();
        {

            Fees f = new Fees();
            f.setName("水费");
            f.setCapital(100);
            f.setLateFees(0);
            fees.add(f);

        }
        {

            Fees f = new Fees();
            f.setName("燃气费");
            f.setCapital(100);
            f.setLateFees(0);
            fees.add(f);

        }
        {

            Fees f = new Fees();
            f.setName("电费");
            f.setCapital(100);
            f.setLateFees(0);
            fees.add(f);

        }

        adapter.setData(fees);
        mRecyclerView.setAdapter(adapter);


        mCheck = (CheckBox) findViewById(R.id.check);

        mCheck.setOnCheckedChangeListener((compoundButton, b) -> {

            mCheck.setTextColor(getResources().getColor(b ? R.color.dark : R.color.white));
            mCheck.setText( b ? "取消全选": "全选" );
            for (Fees fee : fees) {
                fee.setCheck(b);            }
            adapter.notifyDataSetChanged();

        });
    }

    public void ok(View view) {

new ChoosePayTeypDialog(this).show();
    }

    class Fees {
        private boolean check;
        private String name;
        private float capital;
        private float lateFees;
        private float total;

        public boolean isCheck() {
            return check;
        }

        public void setCheck(boolean check) {
            this.check = check;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public float getCapital() {
            return capital;
        }

        public void setCapital(float capital) {
            this.capital = capital;
        }

        public float getLateFees() {
            return lateFees;
        }

        public void setLateFees(float lateFees) {
            this.lateFees = lateFees;
        }

        public float getTotal() {
            return getCapital() + getLateFees();
        }

        public void setTotal(float total) {
            this.total = total;
        }
    }
}
